package com.siwei.mes.service.impl;

import com.siwei.mes.common.util.DateHelper;
import com.siwei.mes.entity.OrderDynamic;
import com.siwei.mes.enums.DynamicEnum;
import com.siwei.mes.mapper.OrderDynamicMapper;
import com.siwei.mes.service.AuthService;
import com.siwei.mes.service.OrderDynamicService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 工单动态(OrderDynamic)表服务实现类
 *
 * @author makejava
 * @since 2023-12-05 09:31:36
 */
@Slf4j
@Service
public class OrderDynamicServiceImpl implements OrderDynamicService {
    @Resource
    private OrderDynamicMapper orderDynamicMapper;
    @Resource
    private AuthService authService;

    @Override
    public List<OrderDynamic> getAll(OrderDynamic entity) {
        return orderDynamicMapper.getList(entity);
    }

    @Override
    public void insertLog(Long orderId, DynamicEnum dynamicEnum) {
        if (orderId == null) {
            log.info("工单id为空，不插入动态");
            return;
        }
        OrderDynamic orderDynamic = new OrderDynamic();
        orderDynamic.setOrderId(orderId);
        orderDynamic.setDataAction(dynamicEnum.getDataAction());
        orderDynamic.setDataTime(new Date());
        orderDynamic.setDataType(dynamicEnum.getDataType());
        String userName = authService.getLoginUserName() == null ? "" : authService.getLoginUserName();
        orderDynamic.setUserId(authService.getLoginUserId());
        orderDynamic.setUserName(userName);
        orderDynamic.setRemark(userName + dynamicEnum.getDataAction() + dynamicEnum.getDataType() + "  " + DateHelper.formatDateTime(new Date()));
        orderDynamicMapper.insertSelective(orderDynamic);
    }

    @Override
    public void insertLogClock(Long orderId,Long clockId, DynamicEnum dynamicEnum) {
        if (orderId == null) {
            log.info("工单id为空，不插入动态");
            return;
        }
        OrderDynamic orderDynamic = new OrderDynamic();
        orderDynamic.setOrderId(orderId);
        orderDynamic.setClockId(clockId);
        orderDynamic.setDataAction(dynamicEnum.getDataAction());
        orderDynamic.setDataTime(new Date());
        orderDynamic.setDataType(dynamicEnum.getDataType());
        String userName = authService.getLoginUserName() == null ? "" : authService.getLoginUserName();
        orderDynamic.setUserId(authService.getLoginUserId());
        orderDynamic.setUserName(userName);
        orderDynamic.setRemark(userName + dynamicEnum.getDataAction() + dynamicEnum.getDataType() + "  " + DateHelper.formatDateTime(new Date()));
        orderDynamicMapper.insertSelective(orderDynamic);
    }
}
